<?php
/**
 * 角色模型
 * panglishan
 * 2020.07.27
 */
namespace App\Models\MainDB;

use Illuminate\Database\Eloquent\Model;

class AdminRole extends Model{

    protected $table = 'ln_admin_nrole';

    protected $primaryKey = 'role_id';

    protected $fillable = [
        'role_id',      //角色编号
        'role_name',    //角色名称
        'role_memo',     //角色备注
        'add_time',     //添加时间
        'status',       //状态1正常2禁用
        'menu_arr',     //菜单ID字符串
        'permission_arr',   //权限ID字符串
    ];

    public $timestamps = false;

    public function admin()
    {
        return $this->hasMany('App\Models\MainDB\Admin', 'role_id', 'role_id');
    }

    public function getAllSubMenuIdStr()
    {
        $menuStr = $this->getAttribute("menu_arr");
        if (empty($menuStr)) {
            return "";
        }

        $allMenuData = app('model_menu')->orderBy('sort', 'desc')->get()->toArray();

        $tempMap = array_column($allMenuData, null, "menu_id");

        $menuArr = explode("," , $menuStr);
        $resultArr = [];
        foreach ($menuArr as $item) {
            if (isset($tempMap[$item])) {
                $resultArr[] = $item;
            }
        }
        return implode(",", $resultArr);
    }

    public function isExist($roleId)
    {
        return $this->select('role_id')->where('role_id', $roleId)->where('is_delete', 0)->first();
    }

}
